package controller;

import dao.QDao;
import model.q;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

public class searchController extends HttpServlet {
    public searchController(){
        super();
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String s = (String)request.getParameter("s");
        if(s == null||s.length()==0){
            request.setAttribute("error","please input your question");
            request.getRequestDispatcher("/qa1.jsp").forward(request,response);
        }
       else {
            QDao dao = new QDao();
            try{

                List<q> qlist;
                if(s == null){ qlist = dao.findAll(); }
                else{ qlist = dao.findbyquestion(s);}
                request.setAttribute("qlist",qlist);
                request.getRequestDispatcher("/qa1.jsp").forward(request,response);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
